<template>
  <div class="container">
    <div class="searchForm">
      <el-form ref="wlForm" :model="form" label-width="80px">
        <el-row :gutter="10">
          <el-col :span="8">
            <el-form-item style="width:100%" label="发布时间">
              <!-- <el-input v-model="form.filter.fbsj"></el-input> -->
              <div style="display:flex;width:100%">
                <el-date-picker
                  v-model="form.filter.fbkssj"
                  type="date"
                  placeholder="开始日期"
                  size="mini"
                  value-format="yyyy-MM-dd"
                  :picker-options="pickerOptionsStart"
                ></el-date-picker>
                <span style="margin: 0 10px">至</span>
                <el-date-picker
                  v-model="form.filter.fbjssj"
                  type="date"
                  placeholder="结束日期"
                  value-format="yyyy-MM-dd"
                  size="mini"
                  :picker-options="pickerOptionsEnd"
                ></el-date-picker>
              </div>

              <!-- <el-date-picker
                style="width:100%"
                v-model="getTimeData"
                value-format="yyyy-MM-dd"
                type="daterange"
                range-separator="至"
                start-placeholder="开始日期"
                end-placeholder="结束日期"
                @change="dataChange"
              ></el-date-picker>-->
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="能力名称">
              <el-input v-model="form.filter.nlmc" clearable></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item style="width:100%" label="使用状态">
              <el-select
                style="width:100%"
                v-model="form.filter.fbzt"
                filterable
                clearable
                placeholder="请选择"
              >
                <el-option
                  v-for="item in stateList"
                  :key="item.label"
                  :label="item.label"
                  :value="item.label"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row :gutter="10" type="flex" justify="space-between">
          <el-col :span="8">
            <el-form-item label="发布单位">
              <el-input v-model="form.filter.dwbm" clearable></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="关键字">
              <el-input v-model="form.filter.gjc" clearable>
                <el-button @click="keywordsClick" slot="append" icon="el-icon-search"></el-button>
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8" style="text-align:right">
            <el-button type="primary" @click="changePageNum(1)">查询</el-button>
          </el-col>
        </el-row>
      </el-form>
    </div>
    <div class="tabs-lb">
      <!-- <span>能力发布</span> -->
      <el-radio-group class="btn-operation" v-model="switchState">
        <el-radio-button :label="true">列表</el-radio-button>
        <el-radio-button :label="false">卡片</el-radio-button>
      </el-radio-group>
      <button @click="addNl" class="btn_operation_add">
        <img src="~@/assets/img/grzx/add.png" alt />
        新增能力
      </button>
    </div>
    <!-- 能力列表图 -->
    <div class="echart-list" v-if="!this.switchState">
      <!-- <div class="search">
        <el-input class="search-input" v-model="form.filter.gjc" placeholder="搜索能力">
          <el-button @click="changePageNum(1)" slot="append" icon="el-icon-search"></el-button>
        </el-input>
        <el-select v-model="form.orderBy" @change="changePageNum(1)">
          <el-option v-for="item in orderbyList" :key="item.bm" :label="item.mc" :value="item.bm"></el-option>
        </el-select>
      </div>-->
      <div class="table-list">
        <nl-card
          ref="cardRef"
          class="nl-card"
          v-for="item in echartData"
          :key="item.jkbh"
          :data="item"
          type="wdfb"
          @clickRow="clickRow"
          @switchChange="switchChange"
        ></nl-card>
      </div>
    </div>
    <!-- 能力列表 -->
    <div class="table-list" v-else-if="this.switchState">
      <div class="table-style">
        <el-table
          :data="tableData"
          border
          stripe
          style="min-height:550px"
          @sort-change="sortChange"
        >
          <!-- <el-table-column
            v-for="item in tableHead"
            :prop="item.prop"
            :key="item.prop"
            :label="item.label"
            align="center"
            :show-overflow-tooltip="true"
          ></el-table-column>-->
          <el-table-column prop="jkmc" label="能力名称" align="center" :show-overflow-tooltip="true">
            <template slot-scope="scope">
              <div class="link" @click="enterInfo(scope.row)">{{ scope.row.jkmc }}</div>
            </template>
          </el-table-column>
          <el-table-column prop="jkms" label="能力概述" align="center" :show-overflow-tooltip="true"></el-table-column>
          <el-table-column
            prop="fbsj"
            label="发布时间"
            sortable="custom"
            align="center"
            :show-overflow-tooltip="true"
          ></el-table-column>
          <el-table-column prop="cjdwmc" label="发布单位" align="center" :show-overflow-tooltip="true"></el-table-column>
          <el-table-column prop="cjrymc" label="发布人" align="center" :show-overflow-tooltip="true"></el-table-column>
          <el-table-column
            prop="jrdycs"
            label="今日调用次数"
            align="center"
            sortable="custom"
            :show-overflow-tooltip="true"
          ></el-table-column>
          <el-table-column
            prop="zdycs"
            label="总调用次数"
            sortable="custom"
            align="center"
            :show-overflow-tooltip="true"
          ></el-table-column>
          <el-table-column
            prop="fbzt"
            label="使用状态"
            sortable="custom"
            align="center"
            :show-overflow-tooltip="true"
          >
            <template slot-scope="scope">
              <el-link type="success" v-if="scope.row.fbzt === '已发布'">{{scope.row.fbzt}}</el-link>
              <el-link type="info" v-if="scope.row.fbzt === '未发布'">{{scope.row.fbzt}}</el-link>
              <el-link type="danger" v-if="scope.row.fbzt === '已停用'">{{scope.row.fbzt}}</el-link>
            </template>
          </el-table-column>
          <el-table-column label="操作">
            <template slot-scope="scope">
              <el-link type="primary" @click="handleNl(scope.row)">修改</el-link>
              <!-- <el-link type="primary" @click="handleCs(scope.row)">测试</el-link> -->
              <el-link
                type="primary"
                @click="handleTy(scope.row.jkbh)"
                v-if="scope.row.fbzt === '已发布'"
              >停用</el-link>
              <el-link
                type="primary"
                @click="handleQy(scope.row.jkbh)"
                :disabled="!scope.row.jkflbh"
                v-else-if="scope.row.fbzt !== '已发布'"
              >启用</el-link>
              <el-link
                :disabled="scope.row.fbzt !=='已停用'"
                type="primary"
                @click="deleteRow(scope.row)"
              >删除</el-link>
            </template>
          </el-table-column>
        </el-table>
      </div>
    </div>
    <!-- 分页 -->
    <div class="page">
      <el-pagination
        background
        @size-change="changePageSize"
        @current-change="changePageNum"
        :current-page="form.pageNum"
        :page-sizes="[10, 20, 50, 100]"
        :page-size="form.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      ></el-pagination>
    </div>
  </div>
</template>
<script>
import jkglService from "@/api/jkgl";
import nlCard from "../../../components/nl-card.vue";

export default {
  components: { nlCard },
  data() {
    return {
      tableData: [],
      switchState: false,
      echartData: [],
      // 日期
      getTimeData: '',
      form: {
        pageNum: 1,
        pageSize: 10,
        filter: {
          dwbm: '',
          fbjssj: '',
          fbkssj: '',
          fbzt: '',
          gjc: '',
          nlmc: '',
        },
        // fbsj DESC
        orderBy: ''
      },
      // 使用状态
      stateList: [
        {
          label: '已发布',
        },
        {
          label: '待分类',
        },
        {
          label: '已停用',
        },
      ],
      total: 0,
      pickerOptionsStart: {
        disabledDate: (time) => {
          let endDateVal = this.form.filter.fbjssj;
          if (endDateVal) {
            return (
              time.getTime() > new Date(endDateVal).getTime()
            );
          }
        },
      },
      pickerOptionsEnd: {
        disabledDate: (time) => {
          let beginDateVal = this.form.filter.fbkssj;
          if (beginDateVal) {
            return (
              time.getTime() <
              new Date(beginDateVal).getTime() - 1 * 24 * 60 * 60 * 1000
            );
          }
        },
      },
    };
  },
  mounted() {
    // 分页
    this.changePageNum(1);
    // 获取能力发布统计图
    this.getNlfbtjt();
  },
  methods: {
    //启用
    handleQy(data) {
      const params = { jkbh: data };
      jkglService
        .enableJk(params)
        .then((res) => {
          this.$message({
            type: "success",
            message: "启用成功!",
          });
          this.getFbList();
        })
        .catch((err) => {
          !err.isError &&
            this.$message({ type: "warning", message: err.message });
        });
    },
    //停用
    handleTy(data) {
      const params = { jkbh: data };
      jkglService
        .getNldyqk(params)
        .then((res) => {
          if (res.dyzrs !== 0) {
            this.$confirm(
              `当前接口订阅人数为 ${res.dyzrs} 人，是否停用当前接口？`,
              "提示",
              {
                confirmButtonText: "确定",
                cancelButtonText: "取消",
                type: "warning",
              }
            )
              .then(() => {
                const params = { jkbh: data };
                jkglService
                  .updateJkdyty(params)
                  .then((res) => {
                    this.$message({
                      type: "success",
                      message: "停用成功!",
                    });
                    this.getFbList();
                  })
                  .catch((err) => {
                    !err.isError &&
                      this.$message({ type: "warning", message: err.message });
                  });
              })
              .catch(() => {
                this.$message({
                  type: "info",
                  message: "已取消",
                });
              });
          } else {
            const params = { jkbh: data };
            jkglService
              .updateJkdyty(params)
              .then((res) => {
                this.$message({
                  type: "success",
                  message: "停用成功!",
                });
                this.getFbList();
              })
              .catch((err) => {
                !err.isError &&
                  this.$message({ type: "warning", message: err.message });
              });
          }
        })
        .catch((err) => {
          !err.isError &&
            this.$message({ type: "warning", message: err.message });
        });
    },
    //修改
    handleNl(data) {
      this.$router.push({
        path: "/nlfb",
        query: {
          cs: data.jkbh,
        },
      });
    },
    /*
     * 获取已发布能力列表
     */
    getFbList() {
      const params = { ...this.form };
      jkglService
        .queryWfbdjk(params)
        .then((res) => {
          this.echartData = res.list;
          this.tableData = res.list;
          this.total = res.total;
        })
        .catch((err) => {
          !err.isError &&
            this.$message({ type: "warning", message: err.message });
        });
    },

    /*
     * 删除能力
     */
    deleteRow(row) {
      this.$confirm(
        `请确认是否删除？`,
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      ).then(() => {
        const params = {
          jkbh: row.jkbh
        };
        jkglService
          .deleteJkdy(params)
          .then((res) => {
            this.$message({
              type: "success",
              message: "删除成功!",
            });
            this.getFbList();
          })
          .catch((err) => {
            !err.isError &&
              this.$message({ type: "warning", message: err.message });
          });
      })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消",
          });
        });
    },

    /*
     * 分页
     */
    changePageNum(val) {
      this.form.pageNum = val;
      this.getFbList();
    },

    /*
     * 分页
     */
    changePageSize(val) {
      this.form.pageSize = val;
      this.form.pageNum = 1;
      this.getFbList();
    },

    /*
     * 跳转发布能力
     */
    addNl() {
      this.$router.push("/nlfb");
    },

    /* 修改 */
    clickRow(type, data) {
      if (type === "editor") {
        this.$router.push({
          path: "/nlfb",
          query: {
            cs: data.jkbh,
          },
        });
      } else if (type === "look") {
        this.$router.push({
          path: "/nlxq",
          query: {
            cs: data.jkbh,
          },
        });
      } else if (type === "test") {
        this.$router.push({
          path: "/zxts",
          query: {
            cs: data.jkbh,
          },
        });
      }
    },

    /*
     * 获取能力发布统计图
     */
    getNlfbtjt() {
      //
    },
    /* 排序变化 */
    sortChange({ prop, order }) {
      if (prop === 'fbsj') {
        switch (order) {
          case 'descending':
            // desc jkmc
            this.form.orderBy = 'fbsj DESC'
            break
          case 'ascending':
            // asc jkmc
            this.form.orderBy = 'fbsj'
            break
          default:
            this.form.orderBy = ''
            break
        }
      }

      if (prop === 'jrdycs') {
        switch (order) {
          case 'descending':
            // desc jkmc
            this.form.orderBy = 'jrdycs DESC'
            break
          case 'ascending':
            // asc jkmc
            this.form.orderBy = 'jrdycs'
            break
          default:
            this.form.orderBy = ''
            break
        }
      }

      if (prop === 'zdycs') {
        switch (order) {
          case 'descending':
            // desc jkmc
            this.form.orderBy = 'zdycs DESC'
            break
          case 'ascending':
            // asc jkmc
            this.form.orderBy = 'zdycs'
            break
          default:
            this.form.orderBy = ''
            break
        }
      }

      if (prop === 'fbzt') {
        switch (order) {
          case 'descending':
            // desc jkmc
            this.form.orderBy = 'fbzt DESC'
            break
          case 'ascending':
            // asc jkmc
            this.form.orderBy = 'fbzt'
            break
          default:
            this.form.orderBy = ''
            break
        }
      }
      this.form.pageNum = 1
      this.getFbList()
    },
    switchChange() {
      this.getFbList();
    },
    handleCs(row) {
      this.$router.push({
        path: "/zxts",
        query: {
          cs: row.jkbh,
        },
      });
    },
    enterInfo(data) {
      this.$router.push({
        path: '/nlxq',
        query: {
          cs: data.jkbh,
        },
      })
    },
    // 日期发生变化的时候
    dataChange(val) {
      if (!val) {
        this.form.filter.fbkssj = '';
        this.form.filter.fbjssj = '';
      } else {
        this.form.filter.fbkssj = val[0];
        this.form.filter.fbjssj = val[1];
      }
    },
    // 点击搜索关键字
    keywordsClick() {
      const params = {
        pageNum: 1,
        pageSize: 10,
        filter: {
          gjc: this.form.filter.gjc,
        },
      };
      jkglService
        .queryWfbdjk(params)
        .then((res) => {
          this.echartData = res.list;
          this.tableData = res.list;
          this.total = res.total;
        })
        .catch((err) => {
          !err.isError &&
            this.$message({ type: "warning", message: err.message });
        });
    }
  },
};
</script>
<style lang="scss" scoped>
.container {
  width: 100%;
  border: 1px solid #cecece;
  background-color: #fff;
  padding: 25px 20px 20px 20px;

  .searchForm {
    border-bottom: 1px solid #cecece;
    padding-bottom: 5px;
  }

  .tabs-lb {
    margin-top: 10px;
    border-bottom: none;
  }

  .echart-list {
    // padding-top: 20px;
    .search {
      width: 100%;
      display: flex;
      justify-content: space-between;
      height: 50px;
      border-bottom: 1px solid #cecece;
      .search-input {
        width: 440px;
        height: 34px;
      }
      .search-select {
        width: 120px;
      }
    }

    .table-list {
      width: 100%;
      // margin-top: 20px;
      padding-top: 10px;
      display: flex;
      flex-wrap: wrap;
      .nl-card {
        margin: 0 20px 20px 0;
        // width: calc(33.33% - 13.33px);
        width: 49.3%;
        // float: left;
        &:nth-child(2n) {
          margin-right: 0;
        }
      }
    }
  }
  // .nl-card {
  //   width: 100%;
  //   height: 150px;
  //   margin-bottom: 20px;
  // }
  .page {
    margin-top: 20px;
    text-align: center;
  }
}

.btn-operation {
  float: right;
  margin-top: 10px;
}

.link {
  color: #4084f0;
  text-decoration: underline;
  cursor: pointer;
}

.table-style {
  margin-top: 20px;
  background-color: #fff;
}
</style>
